package com.example.a2032.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.a2032.R
import com.example.a2032.activity.ChatActivity
import com.example.a2032.db.DataDbHelper
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMConversation
import com.hyphenate.chat.EMMessage
import com.hyphenate.chat.EMTextMessageBody
import com.hyphenate.easeui.constants.EaseConstant
import com.hyphenate.easeui.utils.EaseCommonUtils
import com.hyphenate.easeui.utils.EaseDateUtils
import com.hyphenate.easeui.utils.EaseSmileUtils
import java.util.*

class LanguageAdapter(val languageNames: Array<String>) : RecyclerView.Adapter<LanguageAdapter.UnreadMsgViewHolder>() {

    class UnreadMsgViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var tvName = itemView.findViewById<Button>(R.id.btLanguageName)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UnreadMsgViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_language, parent, false);
        return UnreadMsgViewHolder(view);
    }

    var selectedPosition = -1;
    override fun onBindViewHolder(holder: UnreadMsgViewHolder, position: Int) {
        holder.tvName.text = languageNames[position]
        holder.tvName.setOnClickListener {
            selectedPosition=position
            clickListener?.let {
                clickListener?.onItemClick(position)
            }
            notifyDataSetChanged()
        }
        when (position) {
            selectedPosition -> {
                holder.tvName.setBackgroundResource(R.drawable.language_selected)
            }
            else -> {
                holder.tvName.setBackgroundColor(Color.TRANSPARENT)
            }
        }
    }


    override fun getItemCount(): Int {
        return languageNames.size
    }

    var clickListener: OnItemClickListener? = null;
    fun setItemClickListener(clickListener: OnItemClickListener) {
        this.clickListener = clickListener;
    }

    public interface OnItemClickListener {
        fun onItemClick(position: Int) {
        };
    }
}